package com.visikard.ui.adapter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.visikard.application.Constant;
import com.visikard.item.ItemMerchantHistory;
import com.visikard.ui.R;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;
import com.visikard.utility.Utils;

public class MerchantHistoryAdapter extends ArrayAdapter<ItemMerchantHistory> {

	SimpleDateFormat dateOnlyFormatter = new SimpleDateFormat(Constant.TIME_FORMAT_STR_FULLMONTH_DATE, Locale.US);
	SimpleDateFormat timeFormatter = new SimpleDateFormat(Constant.TIME_FULL_FORMAT_STR, Locale.US);

	public MerchantHistoryAdapter(Context context, List<ItemMerchantHistory> objects) {
		super(context, R.layout.merchant_transaction_history_item, objects);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (convertView == null) {
			view = LayoutInflater.from(getContext()).inflate(R.layout.merchant_transaction_history_item, parent, false);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.avatar = (ImageView) view.findViewById(R.id.image_avatar);
			viewHolder.name = (TextView) view.findViewById(R.id.txt_name);
			viewHolder.desc = (TextView) view.findViewById(R.id.txt_desc);
			viewHolder.content = (TextView) view.findViewById(R.id.txt_content);
			viewHolder.time = (TextView) view.findViewById(R.id.txt_time);
			viewHolder.header = view.findViewById(R.id.header_container);
			viewHolder.headerDate = (TextView) view.findViewById(R.id.txt_header_date);
			view.setTag(viewHolder);
		}

		ViewHolder holder = (ViewHolder) view.getTag();
		ItemMerchantHistory item = getItem(position);

		if (!item.getImage().isEmpty())
			UILImageUtils.loadImage(holder.avatar, // image view
					item.getImage(), // url
					1, // size of view
					BitmapUtils.ROUND_NONE, // round all
					0, // round size
					0, // shadow
					0, // shadow resource
					0, 0);

		holder.name.setText(item.getName());
		holder.content.setText(item.getContent());
		holder.desc.setText(item.getDesc());

		Date today = new Date();
		Date date = new Date(item.getDateTime());
		holder.time.setText(timeFormatter.format(date));

		if (position < 1) {
			String strDate = "";
			if (Utils.compareDateOnly(date, today) == 0) {
				strDate = getContext().getResources().getString(R.string.literal_today) + ", ";
			}
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			strDate += dateOnlyFormatter.format(date);
			holder.headerDate.setText(strDate);
			holder.header.setVisibility(View.VISIBLE);
		}
		else {
			Date date_above = new Date(getItem(position - 1).getDateTime());
			if (Utils.compareDateOnly(date, date_above) != 0) {
				Calendar cal = Calendar.getInstance();
				cal.setTime(date);
				holder.headerDate.setText(dateOnlyFormatter.format(date));
				holder.header.setVisibility(View.VISIBLE);
			}
			else
				holder.header.setVisibility(View.GONE);
		}

		return view;
	}

	class ViewHolder {
		public ImageView avatar;
		public TextView name, desc, content, time;
		public View header;
		public TextView headerDate;
	}

}
